<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Kafka 消费者偏移量管理深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .content-card {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            transition: all 0.3s ease;
        }
        
        .content-card:hover {
            transform: translateY(-2px);
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .code-block {
            background: #2d3748;
            color: #e2e8f0;
            border-radius: 8px;
            padding: 20px;
            overflow-x: auto;
            font-family: 'Consolas', 'Monaco', monospace;
            font-size: 14px;
            line-height: 1.6;
            position: relative;
        }
        
        .code-block::before {
            content: 'Java';
            position: absolute;
            top: 10px;
            right: 10px;
            font-size: 12px;
            color: #718096;
            text-transform: uppercase;
        }
        
        .feature-card {
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
            color: white;
            padding: 30px;
            border-radius: 16px;
            transition: all 0.3s ease;
        }
        
        .feature-card:hover {
            transform: scale(1.02);
            box-shadow: 0 15px 30px rgba(0, 0, 0, 0.2);
        }
        
        .timeline-item {
            position: relative;
            padding-left: 40px;
            margin-bottom: 30px;
        }
        
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 10px;
            top: 8px;
            width: 12px;
            height: 12px;
            background: #667eea;
            border-radius: 50%;
            box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.2);
        }
        
        .timeline-item::after {
            content: '';
            position: absolute;
            left: 15px;
            top: 20px;
            width: 2px;
            height: calc(100% + 10px);
            background: #e2e8f0;
        }
        
        .timeline-item:last-child::after {
            display: none;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 30px 0;
        }
        
        .highlight-box {
            background: linear-gradient(135deg, #ffecd2 0%, #fcb69f 100%);
            padding: 20px;
            border-radius: 12px;
            margin: 20px 0;
            position: relative;
            overflow: hidden;
        }
        
        .highlight-box::before {
            content: '';
            position: absolute;
            top: -50%;
            right: -50%;
            width: 200%;
            height: 200%;
            background: radial-gradient(circle, rgba(255, 255, 255, 0.3) 0%, transparent 70%);
            animation: shimmer 3s infinite;
        }
        
        @keyframes shimmer {
            0% { transform: rotate(0deg); }
            100% { transform: rotate(360deg); }
        }
        
        .drop-cap {
            float: left;
            font-size: 4em;
            line-height: 0.8;
            margin: 0.1em 0.1em 0.1em 0;
            font-weight: 700;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            font-family: 'Noto Serif SC', serif;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl md:text-6xl font-bold mb-6 leading-tight">
                    Kafka 消费者偏移量管理
                    <span class="block text-3xl md:text-4xl mt-4 font-light opacity-90">深度技术解析</span>
                </h1>
                <p class="text-xl md:text-2xl opacity-90 leading-relaxed">
                    探索分布式消息系统中的核心机制，掌握高可用、高性能的偏移量管理策略
                </p>
                <div class="mt-10 flex justify-center space-x-6">
                    <div class="text-center">
                        <i class="fas fa-server text-4xl mb-2"></i>
                        <p class="text-sm">分布式存储</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-sync-alt text-4xl mb-2"></i>
                        <p class="text-sm">故障恢复</p>
                    </div>
                    <div class="text-center">
                        <i class="fas fa-shield-alt text-4xl mb-2"></i>
                        <p class="text-sm">数据可靠性</p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <!-- Introduction -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap">在</span>分布式消息队列系统中，偏移量管理是确保消息不丢失、不重复消费的关键机制。尤其是在 Kafka 这样高吞吐量、高可用性的系统中，偏移量的管理不仅直接影响消息消费的可靠性，还关系到系统的容错性和可扩展性。
            </p>
            <div class="mt-6">
                <img src="https://cdn.nlark.com/yuque/0/2024/png/21449790/1731833500376-d4a19828-533d-4d43-a6fa-fc1fce432f5a.png" 
                     alt="Kafka Architecture" 
                     class="w-full rounded-lg shadow-lg">
            </div>
        </div>

        <!-- Core Concepts -->
        <div class="grid md:grid-cols-2 gap-8 mb-12">
            <div class="feature-card">
                <h3 class="text-2xl font-bold mb-4">
                    <i class="fas fa-bookmark mr-3"></i>什么是偏移量？
                </h3>
                <p class="leading-relaxed">
                    偏移量（Offset）是 Kafka 分区内消息的唯一标识符，标记着消息在分区中的位置。每个分区的偏移量从 0 开始递增，消费者通过偏移量追踪消费进度。
                </p>
            </div>
            <div class="feature-card" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);">
                <h3 class="text-2xl font-bold mb-4">
                    <i class="fas fa-database mr-3"></i>偏移量存储
                </h3>
                <p class="leading-relaxed">
                    Kafka 将偏移量存储在内部主题 __consumer_offsets 中，采用分布式存储和副本机制，确保高可用性和数据持久性。
                </p>
            </div>
        </div>

        <!-- Offset Management Visualization -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">偏移量管理架构</h2>
            <div class="mermaid">
                graph TB
                    subgraph "Kafka Cluster"
                        P1[分区 0<br/>Offset: 0-1000]
                        P2[分区 1<br/>Offset: 0-800]
                        P3[分区 2<br/>Offset: 0-1200]
                        CO[__consumer_offsets<br/>内部主题]
                    end
                    
                    subgraph "Consumer Group"
                        C1[消费者 1]
                        C2[消费者 2]
                        C3[消费者 3]
                    end
                    
                    C1 -->|消费| P1
                    C2 -->|消费| P2
                    C3 -->|消费| P3
                    
                    C1 -->|提交偏移量| CO
                    C2 -->|提交偏移量| CO
                    C3 -->|提交偏移量| CO
                    
                    CO -->|恢复偏移量| C1
                    CO -->|恢复偏移量| C2
                    CO -->|恢复偏移量| C3
                    
                    style P1 fill:#ffd93d,stroke:#333,stroke-width:2px
                    style P2 fill:#ffd93d,stroke:#333,stroke-width:2px
                    style P3 fill:#ffd93d,stroke:#333,stroke-width:2px
                    style CO fill:#6bcf7f,stroke:#333,stroke-width:2px
            </div>
        </div>

        <!-- Offset Commit Strategies -->
        <div class="content-card rounded-2xl shadow-xl p-8 mb-8">
            <h2 class="section-title text-3xl font-bold mb-6 text-gray-800">偏移量提交策略</h2>
            
            <div class="grid md:grid-cols-2 gap-6 mb-8">
                <div class="bg-gradient-to-br from-purple-50 to-pink-50 p-6 rounded-xl">
                    <h3 class="text-xl font-bold mb-3 text-purple-800">
                        <i class="fas fa-robot mr-2"></i>自动提交
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-check text-green-500 mr-2"></i>简单易用，无需手动管理</li>
                        <li><i class="fas fa-check text-green-500 mr-2"></i>适合高吞吐量场景</li>
                        <li><i class="fas fa-times text-red-500 mr-2"></i>可能导致消息丢失或重复</li>
                        <li><i class="fas fa-times text-red-500 mr-2"></i>提交时机不可控</li>
                    </ul>
                </div>
                
                <div class="bg-gradient-to-br from-blue-50 to-cyan-50 p-6 rounded-xl">
                    <h3 class="text-xl font-bold mb-3 text-blue-800">
                        <i class="fas fa-hand-paper mr-2"></i>手动提交
                    </h3>
                    <ul class="space-y-2 text-gray-700">
                        <li><i class="fas fa-check text-green-500 mr-2"></i>精确控制提交时机</li>
                        <li><i class="fas fa-check text-green-500 mr-2"></i>确保消息处理成功后提交</li>
                        <li><i class="fas fa-times text-red-500 mr-2"></i>需要额外的编程工作</li>
                        <li><i class="fas fa-times text-red-500 mr-2"></i>可能影响吞吐量</li>
                    </ul>
                </div>
            </div>

            <!-- Code Example -->
            <div class="mb-8">
                <h4 class="text-lg font-semibold mb-4 text-gray-700">
                    <i class="fas fa-code mr-2"></i>手动提交偏移量示例
                </h4>
                <div class="code-block">
// 同步提交
consumer.commitSync();

// 异步提交
consumer.commitAsync((offsets, exception) -> {
    if (exception != null) {
        log.error("Commit failed for offsets {}", offsets, exception);
    }
});

// 基于时间戳回溯
long timestamp = System.currentTimeMillis() - 3600000; // 1小时前
Map<TopicPartition, OffsetAndTimestamp> offsets = 
    consumer.offsetsForTimes(Collections.singletonMap(partition, timestamp));
OffsetAndTimestamp offset = offsets.get(partition);
if (offset != null) {
    consumer.seek(partition, offset.offset());
}
                </div>
            </div>
        </div>

        <!-- Recovery Mechanism